<?php
/*
 * @Author: your name
 * @Date: 2019-11-19 14:04:28
 * @LastEditTime : 2019-12-30 17:57:00
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /ahsmp_3/application/common/model/HostRule.php
 */

namespace app\common\model;

use think\Model;
class HostRule extends Model 

{
    public $STATUS_ON = 1;//开启
    public $STATUS_OFF = 0;//关闭
    public $TYPE_HANDLE = 1;//用户自定义添加
    public $DIRECTION_OUT = 1;//出站流量
    /**
     * @name: 添加规则来自于 学习数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 18:34:05
     */
    function insertFromLearnData ($host_id, $learn_id, $process, $have_mac)
    {
        $prefix = config('database.prefix');
        $date = date('Ymd');
        $sql = 'INSERT IGNORE INTO ' . $prefix . 'host_rule
            (`host_id`, `alias`, `direction`, `protocols`, `process`, `status`, `type`, `learn_id`, `have_mac_addr`, `src_ip_s`,`src_ip_e`,`dest_ip_s`,`dest_ip_e`, `src_port_s`,`src_port_e`,`dest_port_s`,`dest_port_e`,`mac_addr`)
            SELECT 
            `host_id`, CONCAT(' . $date . ', `id`), `direction`, `protocols`, ' . $process . ', 1, 2, `id`, ' . $have_mac . ', INET_ATON(`src_ip`), INET_ATON(`src_ip`), INET_ATON(`dest_ip`), INET_ATON(`dest_ip`), 0, 65535, `dest_port`, `dest_port`, if(`direction`=1, `dest_mac`, `src_mac`) 
            FROM ' . $prefix . 'host_learn_data where id IN (' . $learn_id . ') AND host_id = ' . $host_id;

        db()->query($sql);
    }
}